package com.baidu.health.service.impl;

import com.baidu.health.mapper.TbAilmentFatorMapper;
import com.baidu.health.mapper.TbAilmentMapper;
import com.baidu.health.pojo.ResultResponse;
import com.baidu.health.pojo.TbAilment;
import com.baidu.health.pojo.TbAilmentFator;
import com.baidu.health.service.TbAilmentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbAilmentServiceImpl extends ServiceImpl<TbAilmentMapper, TbAilment>
    implements TbAilmentService{
    @Autowired
    private TbAilmentMapper tbAilmentMapper;
    @Autowired
    private TbAilmentFatorMapper tbAilmentFatorMapper;
    @Override
    public ResultResponse ailmentSelect(TbAilment tbAilment) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if(!StringUtils.isEmpty(tbAilment.getAilmentName())){
            queryWrapper.like("ailment_name",tbAilment.getAilmentName());
        }
        List<TbAilment> list = tbAilmentMapper.selectList(queryWrapper);
        return ResultResponse.SUCCESS(list);
    }

    @Override
    public ResultResponse ailmentUpdate(TbAilment tbAilment) {
        tbAilmentMapper.updateById(tbAilment);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse ailmentInsert(TbAilment tbAilment) {
        tbAilment.setCreateTime(new Date());
        tbAilmentMapper.insert(tbAilment);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse ailmentDeleted(Integer ailmentId) {
        tbAilmentMapper.deleteById(ailmentId);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("ailment_id",ailmentId);
        List<TbAilmentFator> list = tbAilmentFatorMapper.selectList(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            tbAilmentFatorMapper.deleteById(list.get(i).getRiskId());
        }
        return ResultResponse.SUCCESS();
    }
}




